var prefix = ctx + "admin/job";

$(function() {
    var options = {
        url: prefix + "/page",
        updateUrl: prefix + "/goEditJob",
        removeUrl: prefix + "/{id}",
        sortName: "createTime",
        sortOrder: "desc",
        modalName: "调度任务",
        columns: [{
                title: "序号",
                formatter: function (value, row, index) {
             	    return $.table.serialNumber(index);
                }
            }, {
    	    field: 'jobName',
    	    title: '任务名称',
    	    formatter: function (value, row, index) {
    	    	return '<a href="javascript:$.operate.view(' + "'" + row.jobId + "'" + ')">' + value + '</a>';
            }
    	},{
    	    field: 'jobGroup',
    	    title: '任务组名'
    	},{
			field : 'jobStatus',
			title : '任务状态',
			formatter : function(value, row, index) {
				return $.table.selectDictLabel(statusDatas, value);
			}
		},{
			field : 'jobExecuteStatus',
			title : '执行状态',
			formatter : function(value, row, index) {
				return $.table.selectDictLabel(executeDatas, value);
			}
		},{
			field : 'jobType',
			title : '类型',
			formatter : function(value, row, index) {
				return $.table.selectDictLabel(typeDatas, value);
			}
		},{
			field : 'misfirePolicy',
			title : '错失执行策略',
			formatter : function(value, row, index) {
				return $.table.selectDictLabel(policyeDatas, value);
			}
		},{
    	    field: 'startTime',
    	    title: '首次执行时间'
    	},{
    	    field: 'previousTime',
    	    title: '上次执行时间'
    	},{
    	    field: 'nextTime',
    	    title: '下次执行时间'
    	},{
			field : 'createTime',
			title : '创建时间',
			sortable : true
		},{
			field : 'createBy',
			title : '创建人'
		},{
            title: '操作',
            formatter: function (value, row, index) {
                var actions = [];
                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.jobId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
//                actions.push('<a class="btn btn-danger btn-xs ' + deleteFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.jobId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                actions.push('<a class="btn btn-danger btn-xs ' + deleteFlag + '" href="javascript:void(0)" onclick="removeQuartz(' + row.jobId+ ',' + row.jobStatus + ')"><i class="fa fa-remove"></i>删除</a> ');
                var more = [];
                more.push("<a class='btn btn-default btn-xs " + executeFlag + "' href='javascript:void(0)' onclick='runJob(" + row.jobId + "," + row.jobStatus + ")'><i class='fa fa-play-circle-o'></i> 执行一次</a> ");
                more.push("<a class='btn btn-default btn-xs " + startFlag + "' href='javascript:void(0)' onclick='startJob(" + row.jobId + ")'><i class='glyphicon glyphicon-play'></i>启动</a> ");
                more.push("<a class='btn btn-default btn-xs " + shutdownFlag + "' href='javascript:void(0)' onclick='shutdownJob(" + row.jobId + ")'><i class='glyphicon glyphicon-pause'></i>暂停</a> ");
                more.push("<a class='btn btn-default btn-xs' href='javascript:void(0)' onclick='jobLog(" + row.jobId + ")'><i class='fa fa-list'></i>调度日志</a>");
                actions.push('<a class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');
                return actions.join('');
            }
        }]
    };
    $.table.init(options);
});

/* 暂停全部任务 */
function shutdownAllJob() {
	$.modal.confirm("确认要暂停全部任务吗？", function() {
		$.operate.post(prefix + "/shutdown-jobs");
    })
}

/* 启动全部任务 */
function startAllJob() {
	$.modal.confirm("确认要启动全部任务吗？", function() {
		$.operate.post(prefix + "/start-jobs");
    })
}

/* 重置全部任务 */
function refreshJob() {
	$.modal.confirm("确认要重置全部任务吗？", function() {
		$.operate.post(prefix + "/refresh-jobs");
    })
}

/* 立即执行一次 */
function runJob(jobId,jobStatus) {
	if (jobStatus == 1){
		 $.modal.alertWarning("请先启动后再执行");
		 return
	}
	$.modal.confirm("确认要立即执行一次任务吗？", function() {
		$.operate.post(prefix + "/run-job/" + jobId);
    })
}

/* 启动任务 */
function startJob(jobId) {
	$.modal.confirm("确认要启动任务吗？", function() {
		$.operate.post(prefix + "/start-job/" + jobId);
    })
}

/* 暂停任务 */
function shutdownJob(jobId) {
	$.modal.confirm("确认要暂停任务吗？", function() {
		$.operate.post(prefix + "/shutdown-job/" + jobId);
    })
}

/* 调度日志查询 */
function jobLog(jobId) {
    var url = ctx + 'admin/joblog/index';
    if ($.common.isNotEmpty(jobId)) {
        url += '?jobId=' + jobId;
    }
    $.modal.openTab("调度日志", url);
}

/* 删除任务 */
function removeQuartz(jobId,jobStatus){
	if (jobStatus == 2){
		 $.modal.alertWarning("运行中定时任务不可删除");
		 return
	}
	$.operate.remove(jobId)
}